'use strict';

Object.defineProperty(exports, "__esModule", {
    value: true
});
exports.flatObjAndIgnoreArray = exports.flatObjSafe = exports.flatObjWithEnd = exports.flatObj = exports.flatObjRaw = undefined;

var _typeof2 = require('babel-runtime/helpers/typeof');

var _typeof3 = _interopRequireDefault(_typeof2);

var _isPlainObject = require('lodash/isPlainObject');

var _isPlainObject2 = _interopRequireDefault(_isPlainObject);

var _isArray = require('lodash/isArray');

var _isArray2 = _interopRequireDefault(_isArray);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

var flatObjRaw = exports.flatObjRaw = function flatObjRaw(data) {
    var key = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';
    var doLoop = arguments[2];
    var result = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};

    var item = void 0;
    var deepKey = void 0;
    var preKey = void 0;
    var isEmpty = true;
    for (var k in data) {
        isEmpty = false;
        item = data[k];
        preKey = key || k;
        deepKey = key ? key + '.' + k : k;
        if (doLoop(k, item, preKey, deepKey, result)) {} else {
            result[deepKey] = item;
        }
    }
    if (isEmpty) {
        result[key] = data;
    }
    return result;
};
var flatObj = exports.flatObj = function flatObj(data) {
    var key = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';
    var result = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};

    var doLoop = function doLoop(key, item, preKey, deepKey, result) {
        if ((typeof item === 'undefined' ? 'undefined' : (0, _typeof3.default)(item)) === 'object') {
            flatObj(item, deepKey, result);
            return true;
        }
        return false;
    };
    return flatObjRaw(data, key, doLoop, result);
};
var regEnd = /\.\$end$/;
var flatObjWithEnd = exports.flatObjWithEnd = function flatObjWithEnd(data) {
    var key = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';
    var result = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};

    var doLoop = function doLoop(key, item, preKey, deepKey, result) {
        if (regEnd.test(key)) {
            result[deepKey.replace(regEnd, '')] = item;
            return true;
        }
        if ((typeof item === 'undefined' ? 'undefined' : (0, _typeof3.default)(item)) === 'object') {
            flatObj(item, deepKey, result);
            return true;
        }
        return false;
    };
    return flatObjRaw(data, key, doLoop, result);
};
var flatObjSafe = exports.flatObjSafe = function flatObjSafe(data, key, result) {
    var doLoop = function doLoop(key, item, preKey, deepKey, result) {
        if ((0, _isArray2.default)(item) || (0, _isPlainObject2.default)(item)) {
            flatObjSafe(item, deepKey, result);
            return true;
        }
        return false;
    };
    return flatObjRaw(data, key, doLoop, result);
};
var flatObjAndIgnoreArray = exports.flatObjAndIgnoreArray = function flatObjAndIgnoreArray(data) {
    var key = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';
    var result = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};

    var doLoop = function doLoop(key, item, preKey, deepKey, result) {
        if ((0, _isPlainObject2.default)(item)) {
            flatObjAndIgnoreArray(item, deepKey, result);
            return true;
        }
        return false;
    };
    return flatObjRaw(data, key, doLoop, result);
};